const WXAPI = require('../../wxapi/main')
const CONFIG = require('../../config.js')
    //获取应用实例
var app = getApp()
Page({
    data: {
        inputShowed: true, // 是否显示搜索框
        inputVal: "", // 搜索框内容
        category_box_width: 750, //分类总宽度
        goodsRecommend: [], // 推荐商品
        kanjiaList: [], //砍价商品列表
        pingtuanList: [], //拼团商品列表
        kanjiaGoodsMap: {}, //砍价商品列表

        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        duration: 1000,
        loadingHidden: false, // loading
        userInfo: {},
        swiperCurrent: 0,
        selectCurrent: 0,
        categories: [],
        activeCategoryId: 0,
        goods: [],

        scrollTop: 0,
        loadingMoreHidden: true,

        coupons: [],

        curPage: 1,
        pageSize: 20,
        cateScrollTop: 0,
        active: 0,
        // 筛选
        items: [{
            type: 'filter',
            label: '筛选',
            value: 'filter',
            children: [{
                    type: 'radio',
                    label: '综合',
                    value: 'zonghe',
                    children: [{
                            label: '新品',
                            value: 'xinpin',
                        },
                        {
                            label: '热销',
                            value: 'rexiao',
                        },
                        {
                            label: '促销',
                            value: 'cuxiao',
                        },
                        {
                            label: '优惠',
                            value: 'youhui',
                        },
                    ],
                },
                {
                    type: 'checkbox',
                    label: '类别',
                    value: 'leibie',
                    children: [{
                            label: '食用产品',
                            value: 'shiyong',
                        },
                        {
                            label: '按摩椅',
                            value: 'anmoyi',
                        },
                        {
                            label: '护眼设备',
                            value: 'huyanshebei',
                        },
                        {
                            label: '艾草',
                            value: 'aicao',
                        },
                        {
                            label: '其他',
                            value: 'qita',
                        },
                    ],
                },
                {
                    type: 'checkbox',
                    label: '配送方式',
                    value: 'away',
                    children: [{
                            label: '京东配送',
                            value: '1',
                        },
                        {
                            label: '货到付款',
                            value: '2',
                        },
                        {
                            label: '仅看有货',
                            value: '3',
                        },
                        {
                            label: '促销',
                            value: '4',
                        },
                        {
                            label: '全球购',
                            value: '5',
                        },
                        {
                            label: '配送全球',
                            value: '6',
                        },
                    ],
                },

                {
                    type: 'checkbox',
                    label: '适用场景',
                    value: 'wheel_type',
                    children: [{
                            label: '旅行',
                            value: '0',
                        },
                        {
                            label: '婚庆',
                            value: '1',
                        },
                        {
                            label: '出差',
                            value: '2',
                        },
                        {
                            label: '其他',
                            value: '3',
                        },
                    ],
                },
            ],
            groups: ['001', '002', '003'],
        }, ],
    },
    onLoad() {
        this.getRepos()
        this.showInput()
    },
    onChange(e) {
        const { checkedItems, items } = e.detail
        const params = {}

        console.log(checkedItems, items)

        checkedItems.forEach((n) => {
            if (n.checked) {
                if (n.value === 'updated') {
                    const selected = n.children.filter((n) => n.checked).map((n) => n.value).join(' ')
                    params.sort = n.value
                    params.order = selected
                } else if (n.value === 'stars') {
                    params.sort = n.value
                    params.order = n.sort === 1 ? 'asc' : 'desc'
                } else if (n.value === 'forks') {
                    params.sort = n.value
                } else if (n.value === 'filter') {
                    n.children.filter((n) => n.selected).forEach((n) => {
                        if (n.value === 'language') {
                            const selected = n.children.filter((n) => n.checked).map((n) => n.value).join(' ')
                            params.language = selected
                        } else if (n.value === 'query') {
                            const selected = n.children.filter((n) => n.checked).map((n) => n.value).join(' ')
                            params.query = selected
                        }
                    })
                }
            }
        })

        this.getRepos(params)
    },
    getRepos(params = {}) {
        const language = params.language || 'javascript'
        const query = params.query || 'react'
        const q = `${query}+language:${language}`
        const data = Object.assign({
            q,
        }, params)

        wx.showLoading()
        wx.request({
            url: 'https://api.github.com/search/repositories',
            data,
            success: (res) => {
                console.log(res)

                wx.hideLoading()

                this.setData({
                    repos: res.data.items.map((n) => Object.assign({}, n, {
                        date: n.created_at.substr(0, 7),
                    })),
                })
            },
        })
    },
    onOpen(e) {
        this.setData({
            pageStyle: 'height: 100%; overflow: hidden',
        })
    },
    onClose(e) {
        this.setData({
            pageStyle: '',
        })
    },
    onclick() {
        wx.showToast({
            title: '111',
        })
    },
    onChange(event) {
        wx.showToast({
            title: `切换到标签 ${event.detail.index + 1}`,
            icon: 'none'
        });
    },
    tabClick: function(e) {
        let offset = e.currentTarget.offsetLeft;
        if (offset > 150) {
            offset = offset - 150
        } else {
            offset = 0;
        }
        this.setData({
            activeCategoryId: e.currentTarget.id,
            curPage: 1,
            cateScrollTop: offset
        });
        this.getGoodsList(this.data.activeCategoryId);
    },
    //事件处理函数
    swiperchange: function(e) {
        //console.log(e.detail.current)
        this.setData({
            swiperCurrent: e.detail.current
        })
    },
    toDetailsTap: function(e) {
        wx.navigateTo({
            url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
        })
    },
    tapBanner: function(e) {
        if (e.currentTarget.dataset.id != 0) {
            wx.navigateTo({
                url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
            })
        }
    },
    bindTypeTap: function(e) {
        this.setData({
            selectCurrent: e.index
        })
    },
    onLoad: function(e) {
        wx.showShareMenu({
            withShareTicket: true
        })
        const that = this
        if (e && e.scene) {
            const scene = decodeURIComponent(e.scene)
            if (scene) {
                wx.setStorageSync('referrer', scene.substring(11))
            }
        }

        /**
         * 示例：
         * 调用接口封装方法
         */
        WXAPI.banners({
            type: 'new'
        }).then(function(res) {
            if (res.code == 700) {
                wx.showModal({
                    title: '提示',
                    content: '请在后台添加 banner 轮播图片，自定义类型填写 new',
                    showCancel: false
                })
            } else {
                that.setData({
                    banners: res.data
                });
            }
        }).catch(function(e) {
            wx.showToast({
                title: res.msg,
                icon: 'none'
            })
        })
        WXAPI.goodsCategory().then(function(res) {
            // let categories = [{
            //   id: 0,
            //   icon: '/images/fl.png',
            //   name: "全部"
            // }];
            let categories = [];
            if (res.code == 0) {
                categories = categories.concat(res.data)
            }
            const _n = Math.ceil(categories.length / 2)
                // const _n = Math.ceil(categories.length)
            that.setData({
                categories: categories,
                category_box_width: 150 * _n,
                activeCategoryId: 0,
                curPage: 1
            });
            that.getGoodsList(0);
        })
        WXAPI.goods({
            recommendStatus: 1
        }).then(res => {
            if (res.code === 0) {
                that.setData({
                    goodsRecommend: res.data
                })
            }
        })
        that.getCoupons()
        that.getNotice()
        that.kanjiaGoods()
        that.pingtuanGoods()
    },
    onPageScroll(e) {
        let scrollTop = this.data.scrollTop
        this.setData({
            scrollTop: e.scrollTop
        })
    },
    getGoodsList: function(categoryId, append) {
        if (categoryId == 0) {
            categoryId = "";
        }
        var that = this;
        wx.showLoading({
            "mask": true
        })
        var params = {
            categoryId: categoryId,
            pName: that.data.inputVal,
            page: this.data.curPage,
            pageSize: this.data.pageSize
        }
        wx.request({
                // url: 'http://192.168.0.105:8080/api/products/getList',
                url: 'https://www.brvpmusic.com/api/products/getList',
                method: 'GET',
                data: params,
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    wx.hideLoading()
                    console.log('首页商品获取成功');
                    console.log("");
                    console.log(res.data);
                    if (res.data.status) {
                        var goods = that.data.goods.concat(res.data.body)

                        that.setData({
                            'goods': goods,
                        })
                        if (res.data.body.length < that.data.pageSize) {
                            that.setData({
                                loadingMoreHidden: false
                            })
                        } else {
                            that.setData({
                                loadingMoreHidden: true
                            })
                        }

                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'none'
                        })
                        that.setData({
                            loadingMoreHidden: false
                        })
                    }
                },
                fail: function(res) {
                    wx.hideLoading()
                    console.log('返回失败');
                }
            })
            // return
        WXAPI.goods({
            categoryId: categoryId,
            nameLike: that.data.inputVal,
            page: this.data.curPage,
            pageSize: this.data.pageSize
        }).then(function(res) {
            // wx.hideLoading()
            // if (res.code == 404 || res.code == 700) {
            //     let newData = {
            //         loadingMoreHidden: false
            //     }
            //     if (!append) {
            //         newData.goods = []
            //     }
            //     that.setData(newData);
            //     return
            // }
            // let goods = [];
            // if (append) {
            //     goods = that.data.goods
            // }
            // for (var i = 0; i < res.data.length; i++) {
            //     goods.push(res.data[i]);
            // }
            // that.setData({
            //     loadingMoreHidden: true,
            //     goods: goods,
            // });
        })
    },
    getCoupons: function() {
        var that = this;
        WXAPI.coupons().then(function(res) {
            if (res.code == 0) {
                that.setData({
                    coupons: res.data
                });
            }
        })
    },
    onShareAppMessage: function() {
        return {
            title: '"' + wx.getStorageSync('mallName') + '" ' + CONFIG.shareProfile,
            path: '/pages/index/index?inviter_id=' + wx.getStorageSync('uid')
        }
    },
    getNotice: function() {
        var that = this;
        WXAPI.noticeList({ pageSize: 5 }).then(function(res) {
            if (res.code == 0) {
                that.setData({
                    noticeList: res.data
                });
            }
        })
    },
    toSearch: function() {
        this.setData({
            curPage: 1,
            goods: [],
            loadingMoreHidden: false
        });
        this.getGoodsList(this.data.activeCategoryId);
    },
    onReachBottom: function() {
        if (!this.data.loadingMoreHidden) return
        this.setData({
            curPage: this.data.curPage + 1
        });
        this.getGoodsList(this.data.activeCategoryId, true)
    },
    onPullDownRefresh: function() {
        this.setData({
            curPage: 1
        });
        this.getGoodsList(this.data.activeCategoryId)
        wx.stopPullDownRefresh()
    },
    // 以下为搜索框事件
    showInput: function() {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function() {
        this.setData({
            inputVal: "",
            inputShowed: false
        });
    },
    clearInput: function() {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function(e) {
        this.setData({
            inputVal: e.detail.value
        });
    },
    // 以下为砍价业务
    kanjiaGoods() {
        const _this = this
        WXAPI.kanjiaList().then(function(res) {
            if (res.code == 0) {
                _this.setData({
                    kanjiaList: res.data.result,
                    kanjiaGoodsMap: res.data.goodsMap
                })
            }
        })
    },
    goCoupons: function(e) {
        wx.navigateTo({
            url: "/pages/coupons/index"
        })
    },
    pingtuanGoods() { // 获取团购商品列表
        const _this = this
        WXAPI.goods({
            pingtuan: true
        }).then(res => {
            if (res.code === 0) {
                _this.setData({
                    pingtuanList: res.data
                })
            }
        })
    }
})